/******************************************************************************
*                                                  
*       FAT debug.  This program is to interrogate the functions of the SPI coupled with
a macronix 32Mbit chip. 

Header connector
       
       oo
       oo
miso1  oo  mosi1
clk1   oo  SS1   >>chip to the right                                     
       oo
       oo                                         
       oo
3.3V   oo  gnd
       
      Chip
 
CS  1  8 VCC
SO  2  7 NC                                                
WP  3  6 CLK
gnd 4  5 SI


Some commands

c7  clear all
03  00 00 00  << three bytes specifying address    read memory
06  write enable
02  00 00 00  << three bytes specifying address  then followed by data to write 
9f  read identifier   C2 5E 16
05 read status register
20  00 00 00 sector  erase, specify sector  0-1023
d8  00 00 00 block erase , specify block    0-63


Blocks 0-63   ex block 23 = 23 x 2^16 - 24 x 2^16 - 1
Each block 16 sectors, 2^12 bytes big 

sectors 0-1023    each one 4096 bytes    
ex sector 39  addresses are 39x4096 to 40x4096-1  or in hex  0x27*0x1000 to 0x28*0x1000

4meg of memory runs from 0 to 0x40 00 00           


**************************************************************************/

/* Include Files */
#include "FAT_Debug.h"

/* Global Variables */
UINT8 gau8Minicom[MINICOM_BUFFER_SIZE];
extern UINT8 gu8IntFlags;


/***********************************************************************/
void MiniCom(void)
{
    UINT8   *pu8DataPointer;
    UINT8   u8Selection;
    UINT16  u16Counter;
    UINT16  temp_Counter;
    
    char    add1=0,add2=0;
    
    
    Terminal_Send_String((UINT8*)"\r\n\r\nDemo FAT in S08\r\n");
  
    Terminal_Send_String((UINT8*)"\r\n-Address number? format NNNN four hex digits 0-FFFF  use caps ");
    
    pu8DataPointer=gau8Minicom; //point the pointer to the beginning of the defined buffer file
    
    Terminal_Send_String("\n\r");
//**************Get address value*******************    
    do
    {
        while(!cdc_kbhit());
        *pu8DataPointer=cdc_getch();
        pu8DataPointer++;
        Terminal_Send_Byte((*(pu8DataPointer-1)));   //View the characters as typed
        cdc_process();                              //?
    }while((*(pu8DataPointer-1))!=13);   //check for carriage return

    Terminal_Send_String("\n\r");
    
    
    pu8DataPointer=gau8Minicom;
    
   
   
//****Convert the ascii entered data into numerical data stored in add1 and add2***********************   
    for(u16Counter=0;u16Counter<4;u16Counter++){
   
    if((*pu8DataPointer>='A')&& (*pu8DataPointer<='F'))*pu8DataPointer=*pu8DataPointer-55;
    else if((*pu8DataPointer>='0')&& (*pu8DataPointer<='9'))*pu8DataPointer=*pu8DataPointer-48;
    pu8DataPointer++;
    
    }
    
    pu8DataPointer=gau8Minicom;
    *pu8DataPointer=*pu8DataPointer<<4;
    
    add1=*pu8DataPointer|*(pu8DataPointer+1);
    *(pu8DataPointer+2)=*(pu8DataPointer+2)<<4;
    
    add2=*(pu8DataPointer+2)|*(pu8DataPointer+3);
    
    
 
    
//* Read Sector at specified address 

   SPI_SS=ENABLE;
        SPI_Send_byte(0x06);
    SPI_SS=DISABLE;
     
     
pu8DataPointer=gau8Minicom;   
  
 	  SPI_SS=ENABLE;


	     SPI_Send_byte(0x03);   //read instruction
	     SPI_Send_byte(0x00);
       SPI_Send_byte(add1);
       SPI_Send_byte(add2);
       //SPI_Send_byte(0x9f);
     
       for(u16Counter=0;u16Counter<MINICOM_BUFFER_SIZE;u16Counter++){
    	
	  *pu8DataPointer=SPI_Receive_byte();
	  pu8DataPointer++;
       }

     

  SPI_SS=DISABLE;
  
//* print Sector at specified address  
  pu8DataPointer=gau8Minicom;
   for(u16Counter=0;u16Counter<MINICOM_BUFFER_SIZE;u16Counter++){
   
    if(*pu8DataPointer==0xff)*pu8DataPointer='0';
    else *pu8DataPointer='*';
    Terminal_Send_Byte(*pu8DataPointer); 
    pu8DataPointer++;
    
    } 
      
      
 //********See if editing required? 
   Terminal_Send_String("\n\r");
     
  Terminal_Send_String((UINT8*)"\r\nEdit?  y  or n    ");
       
  while(!cdc_kbhit());
    u8Selection=cdc_getch();
    
    Terminal_Send_String("\n\r");    
      
 
     if(u8Selection=='y'){
  
  Terminal_Send_String((UINT8*)"\r\n  Enter characters  ctrl[z] to quit   \n\r");
  
//********Enter characters into pointer array***********  
        pu8DataPointer=gau8Minicom;
        u16Counter=0;
        do
        {
            while(!cdc_kbhit());
            *pu8DataPointer=cdc_getch();
            u16Counter++;
            Terminal_Send_Byte(*pu8DataPointer);
            cdc_process();
        }while(*(pu8DataPointer++)!=0x1A);              //ctrlz  to stop editing
//********Enter pointer array into Macronix chip***********        
       
        pu8DataPointer=gau8Minicom;   
       
      SPI_SS=ENABLE;
        SPI_Send_byte(0x06);
      SPI_SS=DISABLE;
     
      SPI_SS=ENABLE;


	     SPI_Send_byte(0x02);   //write instruction
	     SPI_Send_byte(0x00);
       SPI_Send_byte(add1);
       SPI_Send_byte(add2);
       //SPI_Send_byte(0x9f);
     
       for(temp_Counter=0;temp_Counter<u16Counter;temp_Counter++){
    	
	  SPI_Send_byte(*pu8DataPointer);
	  pu8DataPointer++;   
       }
       

      SPI_SS=DISABLE;
  
  }
     
      
        
      
     }
 
  
  






/***********************************************************************/
void File_Read(void)
{
    UINT16 u16BufferSize;
    do{
        u16BufferSize=FAT_FileRead(gau8Minicom);
        Terminal_Send_Buffer(gau8Minicom,u16BufferSize);
    }while(u16BufferSize);
}

/***********************************************************************/
void ErrorDisplay(UINT8 u8Error)
{
    Terminal_Send_String((UINT8*)"\r\nError: ");
    Terminal_Send_Byte(u8Error+0x30);
}

